/**
 * Created by Administrator on 2016/5/9.
 */
"use strict";
var LessonID=location.href.split("=")[1];

$(function (){
    if(LessonID){
        showLabsForThislesson(LessonID);
    }
});
function setParentHeight(){
	// 子页面设置父页面iframe高度
    if(window.parent.setcourseexciseIframeheight){
        var ht=$("#container").height();
		ht=ht>150?ht:150;
        window.parent.setcourseexciseIframeheight(ht);
    }
}
function  addLabforThisLesson(e){
  //  window.parent.hideSavebtn(true);
    location.href="CreatLab?lesson="+LessonID;
}

function  showLabsForThislesson(lessonid){
    $.ajax({
        url:"lessons/"+LessonID+"/lessoninfo",
        type:"GET",
       	contentType:"application/json",
        dataType:"json",
        success:function(data){
            if(data.labs!=undefined) {
                //success
                filllessonLabs(data.labs,LessonID);
            }
            else {
                window.parent.childmsg(langThis("lm_queryLabFail"));
            }
        },
        error:function(event){
            window.parent.childmsg(langThis("lm_queryLabFail"));
        }
    });
}
function  filllessonLabs(arr,lessonid){
    var  ht="";
    for(var i in arr){
        var theLab=arr[i];
        ht+="<tr><td>"+theLab.name+"</td><td>"+theLab.description+"</td> <td class=\"tdFixStyle\"><a role=\"button\" class=\"btn btn-link\" id='modifylabId_"+theLab.id+"' onclick='modifyTheLab(this)' >"+langThis("pm_modify")+"</a> <span>/</span> <a role=\"button\" class=\"btn btn-link\"  id='dellabId_"+theLab.id+"'   onclick='deleteTheLab(this)'>"+langThis("pm_delete")+"</a></td></tr>";
    }
    $("#lesson1_tbody").html(ht);
    setTimeout(function(){setParentHeight();},100)
}
function  modifyTheLab (e){
    window.parent.hideSavebtn(true);
    var eid= e.id.split("_")[1];
    var url="modifylab?lab="+eid+"&lid="+LessonID;
    window.location.href=url;
}
function  deleteTheLab(e){
    var title={};
	title.text="<span class='glyphicon glyphicon-exclamation-sign'></span>&nbsp;"+langThis("pm_delete");
	title.css={
		'color':"#fff",
		'background-color':'#d9534f',
		'font-weight': 'bold'
	};
	var masg=langThis("kc_delete");
	var btn1=langThis("kc_confirm");
	var btn2=langThis("pm_cancle");
    var fun1=function(){
        var  theRow=$(e).parents("tr");
        var theLabId= e.id.split("_")[1];
        var url="lessons/"+LessonID+"/labs/"+theLabId;
        $.ajax({
            url:url,
            type:"DELETE",
            dataType:"json",
            success:function(data){
                //success
                if(data){
                    theRow.remove();
                    setTimeout(function(){setParentHeight();},100)
                    window.parent.childmsg(langThis("lm_delLabSuc"));
                }
                else {
                    window.parent.childmsg(langThis("lm_delLabFail"));
                }
            },
            error:function(event){
                window.parent.childmsg(langThis("lm_delLabFail"));
            }
        });
    };
    window.parent.pLayerConfirm(title,masg,btn1,btn2,fun1);
}